package com.zw.utils;

import jakarta.mail.internet.MimeMessage;
import lombok.SneakyThrows;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Arrays;

/**
 * @author by: zyl
 * @ClassName: MailUtil
 * @Description: 发送邮件工具类
 * @Date: 2025/7/2 17:44
 */
@Component
public class MailUtil {

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String from;


    /**
     * 发送普通邮件
     *
     * @param subject
     * @param content
     * @param to
     */
    @SneakyThrows(Exception.class)
    public boolean sendGeneralMail(String subject, String content, String... to) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setText(content);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setTo(to);
        javaMailSender.send(simpleMailMessage);
        return true;
    }

    /**
     * 发送html格式的邮件
     *
     * @param subject
     * @param content
     * @param to
     */
    @SneakyThrows(Exception.class)
    public boolean sendHtmlMail(String subject, String content, String... to) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        javaMailSender.send(mimeMessage);
        return true;
    }


    /**
     * 发送验证码
     *
     * @param code
     * @param to
     * @return
     */
    @SneakyThrows
    public boolean sendEmailCode(String code, String... to) {
        System.out.println("发送验证码:" + Arrays.toString(to));
        Context context = new Context();
        context.setVariable("code", code);
        context.setVariable("subject", "验证码");
        String htmlContent = templateEngine.process("email", context);
        return sendHtmlMail("验证码", htmlContent, to);
    }


    /**
     * 发送账户激活邮箱
     * @param redirectUrl 回调地址
     * @param username 用户名
     * @param email 邮箱
     * @param registerTime 注册时间
     * @param to 接收者
     * @return  boolean
     */
    public boolean sendEmailActivate(String redirectUrl, String username, String email, String registerTime, String...to){
        Context context = new Context();
        context.setVariable("redirectUrl", redirectUrl);
        context.setVariable("username", username);
        context.setVariable("email", email);
        context.setVariable("registerTime", registerTime);
        String htmlContent = templateEngine.process("activate", context);
        return sendHtmlMail("账户激活", htmlContent, to);
    }

}
